package GateWars;

import java.io.BufferedWriter;
import java.io.FileWriter;

public class PageContentParser {
	
	public PageContentParser(){	}
	
	public void parse(String sb){
		String[] temp = sb.split("<TITLE>");
		if(temp.length > 1)
		{
			temp = temp[1].split("</TITLE>");
		}
		//System.out.println(temp[0]);
		if(temp[0].length() >= 17)
		{
			String temp2 = temp[0].substring(17);
			
			/**
			 * Base has been partially parsed, Planets still need to be read.
			 */
			if(temp2.startsWith("Base"))
			{
				BaseParser.parse(sb);
			}
			else if(temp2.startsWith("Battlefield"))
			{
				BFParser.parse(sb);
			}
			else if(temp2.startsWith("Bank"))
			{
				System.out.println(temp2);
			}
			else if(temp2.startsWith("Train"))
			{
				System.out.println(temp2);
			}
			else if(temp2.startsWith("Market Place"))
			{
				System.out.println(temp2);
			}
			else if(temp2.startsWith("Trade Broker"))
			{
				System.out.println(temp2);
			}
			else if(temp2.startsWith("Planets"))
			{
				System.out.println(temp2);
			}
			else if(temp2.startsWith("Stats"))
			{
				System.out.println(temp2);
			}
			else if(temp2.startsWith("Armory"))
			{
				ArmoryParser.parse(sb);
			}
			else if(temp2.startsWith("MotherShip Management"))
			{
				System.out.println(temp2);
			}
			else if(temp2.startsWith("Attack Log"))
			{
				System.out.println(temp2);
			}
			else if(temp2.startsWith("Intel"))
			{
				System.out.println(temp2);
			}
			else if(temp2.startsWith("Alliance Ranks"))
			{
				System.out.println(temp2);
			}
            try{
            // Create file 
            FileWriter fstream = new FileWriter("logFiles\\" + temp2 + ".txt");
                BufferedWriter out = new BufferedWriter(fstream);
            out.write(sb);
            //Close the output stream
            out.close();
            }catch (Exception e){//Catch exception if any
//              System.err.println("Error: " + e.getMessage());
            }
		}
	}
	
	/**
	 * failed because <TD> can contain another <TD> before it ends.
	 */
	/*public String[] getObjectNames(String sb){
		String[] temp = sb.split("<TD");
		String[] response = new String[temp.length];
		for(int i = 0; i < response.length; i++){
			response[i] = temp[i].substring(temp[i].indexOf('>'), temp[i].indexOf("TD>"));
		}
		return null;
	}*/
}
